package com.ld.rightsSystem.service.impl;

import com.ld.rightsSystem.Enum.ResponseEnum;
import com.ld.rightsSystem.exception.DBDataNotFoundException;
import com.ld.rightsSystem.filter.JwtRequestFilter;
import com.ld.rightsSystem.mapper.TPermissionRepository;
import com.ld.rightsSystem.pojo.TPermission;
import com.ld.rightsSystem.service.inf.TPermissionService;
import com.ld.rightsSystem.utils.Utils;
import com.ld.rightsSystem.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author lhb
 * @since 2023-12-27
 */
@Service
public class TPermissionServiceImpl implements TPermissionService {
    @Autowired
    private TPermissionRepository tPermissionRepository;

    @Autowired
    private JwtRequestFilter jwtRequestFilter;

    /**
     * 添加权限
     * @param tPermission
     * @return
     */
    @Override
    public R addTPermission(TPermission tPermission) {
        Long findTPermissionIdByPermissionCode = tPermissionRepository.findTPermissionIdByPermissionCode(tPermission.getPermissionCode());
        if(findTPermissionIdByPermissionCode != null){
            return new R().error(ResponseEnum.PARAMS_BINDING_ERROR,null);
        }
        Long findTPermissionIdByPermissionName = tPermissionRepository.findTPermissionIdByPermissionName(tPermission.getPermissionName());
        if(findTPermissionIdByPermissionName != null){
            return new R().error(ResponseEnum.PARAMS_BINDING_ERROR,null);
        }
        tPermission.setCreateBy(jwtRequestFilter.userNameThreadLocal.get());
        tPermissionRepository.insert(tPermission);
        return new R().OK(null);
    }

    /**
     * 修改权限
     * @param tPermission
     * @return
     */
    @Override
    public R editTPermission(TPermission tPermission) throws DBDataNotFoundException {
        TPermission tPermissionOriginal= tPermissionRepository.findTPermissionByPermissionId(tPermission.getPermissionId());
        if(tPermissionOriginal != null){
            tPermission.setUpdateBy(jwtRequestFilter.userNameThreadLocal.get());
            // 设置操作信息
            tPermission.setUpdateTime(new Date());
            tPermissionRepository.updateTPermission(tPermission);
            return new R().OK(null);
        }else {
            throw new DBDataNotFoundException();
        }
    }


    /**
     * 根据id删除权限
     * @param permissionId
     * @return
     */
    @Override
    public R deleteTPermissionById(Long permissionId) throws DBDataNotFoundException {
        TPermission tPermissionOriginal= tPermissionRepository.findTPermissionByPermissionId(permissionId);
        if(tPermissionOriginal != null){
            tPermissionRepository.deleteTPermissionByPermissionId(permissionId,jwtRequestFilter.userNameThreadLocal.get(),new Date());
            return new R().OK(null);
        }else {
            throw new DBDataNotFoundException();
        }
    }

    /**
     * 分页查询菜单列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public R findTPermissionList(int pageNum, int pageSize,boolean tree) {
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize);
        HashMap<String, List<TPermission>> result = new HashMap<>();
        List tPermissionList = tPermissionRepository.findTPermissionsPageable(pageRequest);
        if(tree == true){
            tPermissionList = Utils.getPermissionList(tPermissionList);
        }
        result.put("list",tPermissionList);
        return new R().OK(result);
    }


    /**
     * 校验字段是否可用
     * @param checkPName
     * @return
     */
    @Override
    public Boolean checkPermissionNameAvailable(String checkPName) {
        if(checkPName.trim().length() > 0){
            Long tPermissionIdByPermissionName = tPermissionRepository.findTPermissionIdByPermissionName(checkPName);
            if(tPermissionIdByPermissionName != null){
                return false;
            }else {
                return true;
            }
        }else {
            return false;
        }
    }

    /**
     * 校验字段是否可用
     * @param checkPCode
     * @return
     */
    @Override
    public Boolean checkPermissionCodeAvailable(String checkPCode) {
        if(checkPCode.trim().length() > 0){
            Long tPermissionIdByPermissionCode = tPermissionRepository.findTPermissionIdByPermissionCode(checkPCode);
            if(tPermissionIdByPermissionCode != null){
                return false;
            }else {
                return true;
            }
        }else {
            return false;
        }

    }

    /**
     * 校验字段是否可用
     * @param checkPName
     * @param checkPCode
     * @return
     */
    @Override
    public R checkFieldAvailable(String checkPName, String checkPCode) {
        Map<String,Boolean> result = new HashMap<>();
        Boolean available = false;
        if(checkPName != null){
            available = checkPermissionNameAvailable(checkPName);
            result.put("checkPName",available);
        }else if(checkPCode != null){
            available = checkPermissionCodeAvailable(checkPCode);
            result.put("checkPCode",available);
        }
        return new R().OK(result);
    }

    /**
     * 根据权限id查询权限详细信息
     * @param permissionId
     * @return
     */
    @Override
    public R findTPermissionInfoById(Long permissionId) throws DBDataNotFoundException {
        TPermission tPermission = null;
        try{
             tPermission = tPermissionRepository.findById(permissionId).get();
        }catch (NoSuchElementException e){
           throw new DBDataNotFoundException();
        }
        HashMap<String, TPermission> result = new HashMap<>();
        result.put("info",tPermission);
        return new R().OK(result);
    }
}
